﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using XA_SESSIONLib;


namespace IRUM.XingAPI
{
    public class loginArgs : EventArgs
    {
        public string code = string.Empty;
        public string message = string.Empty;

        public loginArgs(string code, string Msg)
        {
            this.code = code;
            this.message = Msg;
        }
    }

    public delegate void LoginHandler(object s, loginArgs e);

    public class SessionCtrl : _IXASessionEvents
    {
        public IXASession m_Session;
        protected IConnectionPoint m_icp;
        protected IConnectionPointContainer m_icpc;
        protected int m_dwCookie;
        public event LoginHandler OnLoginResultReceived;

        public SessionCtrl ()
        {
            int m_dwCookie = 0;
            m_Session = new XASession();
            m_icpc = (IConnectionPointContainer)m_Session;
            Guid IID_SessionEvents = typeof(_IXASessionEvents).GUID;
            m_icpc.FindConnectionPoint(ref IID_SessionEvents, out m_icp);
            m_icp.Advise(this, out m_dwCookie);       
        }

        ~SessionCtrl()
        {
            m_Session.Logout();
            m_Session.DisconnectServer();
        }

        private void AddLog(string log)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[SessionCtrl]\t");
            sb.Append(log);

            Debug.Print(sb.ToString());
        }

        #region _IXASessionEvents 멤버

        void _IXASessionEvents.Disconnect()
        {
            AddLog("Disconnect");
        }

        void _IXASessionEvents.Login(string szCode, string szMsg)
        {
            AddLog(szCode + ":" + szMsg);

            if (OnLoginResultReceived != null)
            {
                OnLoginResultReceived(this, new loginArgs(szCode, szMsg));
            }
        }

        void _IXASessionEvents.Logout()
        {
            AddLog("LogOut");
        }

        #endregion


    }
}
